<template>
  <!-- 供应商分类 -->
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :page-no="76"
        :add-active="addActive"
        :del-active="delActive"
        :has-edit="false"
        :has-cancel="false"
        :has-save="false"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" :labelWidth="'125px'" @search="search"></SearchForm>
    </template>
    <template v-slot:table-box>
      <Table @sortChange="changeTableSort" :propList="tableColumn" v-loading="loading"
        :listData="tableData" @handleSelectionChange="handleSelectionChange">
        <template v-slot:operation="{ row }">
          <template v-if="!row.index">
            <el-button
              v-if="judgePermission(76, 'edit')"
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="handleEdit(row)"
            >
              <i class="el-icon-edit-outline"></i> 修改
            </el-button>
            <el-button
              v-if="judgePermission(76, 'delete')"
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(row)"
            >
              <i class="el-icon-delete"></i> 删除
            </el-button>
          </template>
        </template>
      </Table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="dialogTitle + '供应商类型'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :rules="rules"
          :model="formData"
          class="el-form"
          :show-message="isshowmessage"
        >
          <el-form-item label="供应商类型名称:" class="w50p" prop="supplierTypeName">
            <el-input v-model="formData.supplierTypeName" />
          </el-form-item>
          <el-form-item label="备注:" class="w50p" prop="remarks">
            <el-input v-model="formData.remarks" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">取 消</el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >保 存</el-button>
        </div>
      </el-dialog>

      <el-dialog
        id="tableColumnDialog"
        title="配置列"
        :visible.sync="dialogTableColumnVisible"
      >
        <tableColumn
          v-if="dialogTableColumnVisible"
          ref="tableColumn"
          :tableColumn="allTableColumn"
          @cancelColumn="cancelColumn"
        />
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="dialogTableColumnVisible = false">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="saveColumn"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>

    </template>
  </BaseMainSlot>
</template>
<script>
import SearchForm from '@/components/searchForm';
import Table from '@/components/table/table.vue';
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import supplierTypeApi from "@/api/digital-operation/purchase-manage/supplier-type";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";
export default {
  name: "AgrInputManagementtwo",
  components: { SearchForm, Table, ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      tableColumn: [
        { prop: 'supplierTypeName', label: '供应商类型名称' },
        { prop: 'remarks', label: '备注' },
        { prop: 'operation', label: '操作', slotName: 'operation', width: '120' }
      ],
      searchlist: {},
      formConfig: [
        {
          label: '供应商类型名称',
          prop: 'supplierTypeName',
          type: 'input',
        }
      ],
      dialogTitle: null,
      dialogFormVisible: false,
      // 新增的规则校验
      isshowmessage: false,
      rules: {
        supplierTypeName: [
          { required: true, message: "请输入供应商类型名称", trigger: "change" }
        ],
      },
      formData: {},
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {
    nowChanceStau(newval) {
      this.isshowmessage = !!newval;
    },
    dialogFormVisible(v) {
      if (v) {
        this.$nextTick(()=>{
          this.$refs.formData.clearValidate()
        })
      }
    }
  },
  mounted() {
    this.getTableDataList();
  },
  methods: {
    // 获取当前表格信息
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        // sortList: this.sortList || [],
        entity: {
          ...this.searchlist
        }
      };
      this.entity = params.entity
      supplierTypeApi.pageQuery(params).then(res => {
        this.loading = false;
        if (res.code !== 1) {
          this.$message.error(res.msg);
          return
        }
        let data = res.data;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data.content;
        this.addblankmet();
        this.resetActionBar();
      });
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    add() {
      this.formData = {};
      this.openDialog("add");
    },
    // 表格点击
    handleEdit(list) {
      this.formData = { ...list };
      this.openDialog("edit");
    },
    handleSelectionChange(list) {
      this.selectIdList = [];
      this.selectIdList = list.map(item => {
        return item.supplierTypeNo;
      });
      if (
        this.selectIdList &&
        this.selectIdList.length > 0
      ) {
        this.delActive = true;
      } else this.delActive = false;
    },
    delmessage() {
      if (this.nowChanceStau === "del") {
        let params = {
          noList: this.selectIdList
        };
        supplierTypeApi.numberBatchDel(params).then(res => {
          if (res.code === 1) {
            this.getTableDataList();
            this.$message.success("删除成功");
          } else {
            this.$message.error("删除失败");
          }
        });
      }
    },
    handleDelete(data) {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          let params = {
            noList: [data.supplierTypeNo]
          };
          supplierTypeApi.numberBatchDel(params).then(res => {
            if (res.code === 1) {
              this.$message.success("删除成功");
              this.getTableDataList();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message("取消删除");
        });
    },
    save() {
      this.$refs["formData"].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "add") {
            let parone = {
              ...this.formData
            };
            supplierTypeApi.add(parone).then(res => {
              if (res.code === 1) {
                this.closeDialog();
                this.getTableDataList();
                this.$message.success("添加成功");
              } else {
                this.$message.error("添加失败");
              }
            });
          }
          if (this.nowChanceStau === "edit") {
            let params = {
              ...this.formData
            };
            supplierTypeApi.edit(params).then(res => {
              if (res.code === 1) {
                this.closeDialog();
                this.getTableDataList();
                this.$message.success("修改成功");
              } else {
                this.$message.error("修改失败");
              }
            });
          }
        } else {
          this.$message.warning("请填写完整！！！");
        }
      });
    },
    cancel() {
      this.closeDialog();
      this.formData = {};
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogFormAssignVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    },
  }
};
</script>
<style lang="scss" scoped>
.tabhid {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  // background-color: #0cb28c;
}
.zhtabletitle {
  color: #31f7dd;
  margin-bottom: 10px;
}
::v-deep .staff-manage .common-layout .content {
  overflow-y: auto;
}
.upimg {
  ::v-deep .xunzeimage {
    width: 140px !important;
  }
}
.dialog {
  img {
    width: auto;
    max-width: 100%;
  }
}
::v-deep .el-upload--picture-card {
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.1);
  border: 1px solid #6ecae3;
}
::v-deep .el-upload--picture-card {
  position: relative;
}
::v-deep .el-upload--picture-card .el-icon-plus {
  font-size: 34px;
  color: #6ecae3;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
::v-deep .el-dialog__body {
  height: 90%;
  text-align: center;
  // img {
  //   width: 100%;
  // }
}
::v-deep .el-upload__tip {
  display: none;
}
::v-deep .el-form {
  .el-form-item__label {
    min-width: 130px !important;
  }
}
</style>
